package com.hejian.android.a72ggametools.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.BounceInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.net.HttpUtils;

import java.util.Timer;
import java.util.TimerTask;

public class StartActivity extends AppCompatActivity {


    private static final String TAG = "hejiandebug";
    private ImageView imageView;
    private TextView textView;
    private Timer imgTimer;
    private Timer textTimer;
    private float textCurX = -200;
    private float textCurY = 0;
    private float textNextX = -200;
    private float textNextY = 0;
    private float imageCurX = 0;
    private float imageCurY = -400;
    private float imageNextX = 0;
    private float imageNextY = -400;
    private int start = 0;
    private TranslateAnimation translateAnimation;
    private boolean isFirstIn;
    private int width;
    private int height;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        SharedPreferences sharedPreferences = getSharedPreferences("first_share", MODE_PRIVATE);
        isFirstIn = sharedPreferences.getBoolean("isFirstIn", false);
        initView();
        initAnimation();


    }

    private void initAnimation() {
        initTextAnimation();

    }

    private void initTextAnimation() {
        textTimer = new Timer(true);
        textTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(1);
            }
        }, 200);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    playTextAnimation();
                    break;
                case 2:
                    playImgAnimation();
            }
        }
    };

    private void playImgAnimation() {
        start++;
        if (start == 1) {
            imageView.setVisibility(View.VISIBLE);
            int imageHeight = View.MeasureSpec.makeMeasureSpec(0, TextView.MeasureSpec.UNSPECIFIED);
            imageView.measure(0, imageHeight);
            imageHeight = imageView.getMeasuredHeight();
            Log.d(TAG, "playImgAnimation: "+imageHeight);
            imageNextY += height / 2 + imageHeight / 2;
            translateAnimation = new TranslateAnimation(imageCurX, imageNextX, imageCurY, imageNextY);
            translateAnimation.setDuration(900);
            translateAnimation.setFillAfter(true);
            translateAnimation.setInterpolator(new BounceInterpolator());
            imageCurY = imageNextY;
            imageView.startAnimation(translateAnimation);
        } else {
            imgTimer.cancel();
            textTimer.cancel();
            if (HttpUtils.isConnected(getApplicationContext())) {
                if (isFirstIn == false) {
                    Intent intent = new Intent(StartActivity.this, GuideActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Intent intent = new Intent(StartActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            } else {

                Toast.makeText(StartActivity.this, "网络连接有误，请检查您的网络！", Toast.LENGTH_SHORT).show();
            }

        }
    }

    private void playTextAnimation() {
        textView.setVisibility(View.VISIBLE);
        int textwidth = View.MeasureSpec.makeMeasureSpec(0, TextView.MeasureSpec.UNSPECIFIED);
        textView.measure(textwidth, 0);
        textwidth = textView.getMeasuredWidth();
        Log.d(TAG, "playTextAnimation: "+textwidth);
        textNextX += width / 2 + textwidth / 2;
        translateAnimation = new TranslateAnimation(textCurX, textNextX, textCurY, textNextY);
        translateAnimation.setDuration(600);
        translateAnimation.setFillAfter(true);
        textCurX = textNextX;
        translateAnimation.setInterpolator(new OvershootInterpolator());
        textView.startAnimation(translateAnimation);
        initImageAnimation();
    }

    private void initImageAnimation() {
        imgTimer = new Timer(true);
        imgTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(2);
            }
        }, 200, 1200);
    }


    private void initView() {
        WindowManager manager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        width = manager.getDefaultDisplay().getWidth();
        height = manager.getDefaultDisplay().getHeight();
        imageView = (ImageView) findViewById(R.id.image);
        textView = (TextView) findViewById(R.id.text);
    }

}
